package com.example.zhu.mapper;

import com.example.zhu.dto.OrderDTO;
import com.example.zhu.entity.Order;
import com.example.zhu.entity.Orders;
import com.example.zhu.vo.OrderMaxVO;
import com.example.zhu.vo.OrderVO;
import com.example.zhu.vo.OrderVOMax;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {

    @Select("select * from orders where id = #{id}")
    Orders selectById(Integer id);
    @Delete("delete t1,t2 from orders t1,orders_detail t2 where t1.openid = #{openid} and t1.id = t2.order_id")
    void deleteByOpenid(String openid);

    @Delete("delete from take_orders where openid = #{openid}")
    void deleteTakeOrder(String openid);

    @Select("select distinct t1.id,t4.phone outPhone,t1.number,t1.product_id,t1.amount,t1.order_status,t3.phone,t1.create_time from orders t1,take_orders t2,address_book t3,orders_detail t4 where t1.id = t4.order_id and t1.number = t2.number and t2.openid = t3.openid and t3.is_default =1 order by t1.create_time desc ")
    List<OrderVO> selectAll();

    @Select("select t1.id,t1.number,t1.product_id,t1.amount,t1.order_status,t3.phone from orders t1,take_orders t2,address_book t3 where t1.id = #{id} and t1.number = t2.number and t2.openid = t3.openid and t3.is_default =1")
    OrderVO getEditInfo(Integer id);

    @Update("update orders set order_status = #{orderStatus} ,expect_time=#{expectTime} where id = #{id}")
    void updateEditInfo(OrderDTO orderDTO);

    @Select("select * from orders")
    List<Orders> selectAllOrder();

    List<OrderVO> selectByNumberAndPhone(String number, String phone, String outPhone);

    @Select("select distinct t1.*,t2.order_id, take_address, package_info, remark, t2.phone, t2.address,t3.openid as takeOpenid, takeorder_time, take_time, delivery_time,t4.phone as takePhone from orders t1,orders_detail t2,take_orders t3, address_book t4 where t1.id = #{id} and t1.id = t2.order_id and t1.number = t3.number and t3.openid = t4.openid and is_default = 1")
    OrderMaxVO selectAllInfo(Integer id);

    @Select("select distinct t2.image from orders t1,orders_detail t2 where t1.id = t2.order_id and t1.id = #{id}")
    List<String> selectImgs(Integer id);

    @Select("select t1.id,t1.number,t1.product_id,t1.order_status,t1.amount,t1.create_time,t1.expect_time,t1.finish_time,t1.imageOver ,t1.count,t2.phone,t2.take_address,t2.address," +
            "t2.package_info,t2.remark,t2.image from orders t1,orders_detail t2 where t1.id = t2.order_id and t1.number = #{number} ")
    List<Orders> selectTakes(Long number);


    /**
     * 更新订单状态
     * @param id
     * @param cancel
     */
    @Update("update orders set order_status = #{cancel} where id = #{id}")
    void updateCancel(Integer id,Integer cancel);
    @Update("update orders set order_status = #{cancel} where number = #{number}")
    void updateStatus(Long number,Integer cancel);

    @Update("update orders set order_status = #{received} where id = #{id}")
    void updateCollected(Integer id, Integer received);

    @Update("update orders set order_status = #{reached} where id = #{id}")
    void updateReached(Integer id, Integer reached);

    @Update("update orders set order_status = #{completed} where id = #{id}")
    void updateCompleted(Integer id, Integer completed);

    @Update("update orders set update_time = #{now} where id = #{id}")
    void updateTime(Integer id, LocalDateTime now);

    @Update("update orders set finish_time = #{now} where id = #{id}")
    void updateFinishTime(Integer id, LocalDateTime now);

    @Update("update take_orders set take_time = #{now} where number = #{number}")
    void updateTakeTime(Long number, LocalDateTime now);

    @Update("update take_orders set delivery_time = #{now} where number = #{number}")
    void updateDeliveryTime(Long number, LocalDateTime now);

    @Select("select distinct t1.create_time ,amount,expect_time,t2.phone,address from orders t1,orders_detail t2 where t1.id = t2.order_id and t1.create_time>=#{s} and t1.create_time<=#{e} and t1.order_status = 5 order by t1.create_time desc")
    List<OrderVOMax> selectByTime(LocalDateTime s, LocalDateTime e);

    @Update("update orders set imageOver = #{imageOver} where id=#{id}")
    void updateImage(String imageOver,Integer id );

    @Update("update orders set order_status = -2 where id=#{id}")
    void updateFail(Integer id);

    @Select("select distinct t1.*, t2.phone,t2.address,t3.order_type from orders t1,orders_detail t2,product_info t3 where t1.product_id =t3.id and t1.id=t2.order_id and t1.create_time>=#{startTime} order by t1.create_time desc")
    List<Orders> selectOrders(LocalDateTime startTime);

    @Select("select distinct t1.id, number, product_id, amount, order_status, openid, create_time, update_time, expect_time, finish_time,  coupon_id, imageOver, is_use_coupon,count,t2.take_address, package_info, remark, phone, address,t3.order_type from orders t1,orders_detail t2, product_info t3 where t1.id = t2.order_id and t1.id = #{id} and t1.product_id = t3.id")
    OrderMaxVO selectInfo(Integer id);

    @Update("update orders set order_status = #{status}, update_time=#{now} where id = #{id}")
    void update(Integer id, LocalDateTime now,Integer status);


    @Update("update orders set count = #{count},update_time = #{now} where id = #{id}")
    void updateCount(Integer id, Integer count,LocalDateTime now);

    @Select("select  distinct t1.*, t2.phone,t2.address,t3.order_type from orders t1,orders_detail t2,product_info t3 where t1.product_id =t3.id and t1.id=t2.order_id and left(t1.expect_time,5) = #{today} order by t1.create_time desc")
    List<Orders> selectByExpectTime(String today);

    @Select("select  distinct t1.*, t2.phone,t2.address,t3.order_type from orders t1,orders_detail t2,product_info t3 where t1.product_id in(7,8) and t1.product_id =t3.id and t1.id=t2.order_id and left(t1.create_time,10) = #{today} order by t1.create_time desc")
    List<Orders> selectByCreateTime(String today);

    @Select("select t1.id,t1.number,t1.order_status,t1.imageOver,t1.count,t2.remark,t2.phone,t2.package_info,t2.address, GROUP_CONCAT(t2.image SEPARATOR ' ') AS images from orders t1,orders_detail t2 where t1.id = t2.order_id and t1.number = #{number} group by t2.order_id ")
    Order selectByNumber(Long number);

    List<Order> selectByNumber_(List<Long> numberList);

    @Update("update orders set product_id = 13,order_status = 1 where id = #{id}")
    void updateById(Integer id);

    @Update("update orders set error_msg = #{errorMsg} where id = #{id}")
    void updateMsg(Integer id, String errorMsg);

    @Update("update orders set order_status = #{i},finish_time = #{now} where id = #{id}")
    void updateRefund(Integer id, int i, LocalDateTime now);
}
